﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using SuperScrollView;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;
using Random = UnityEngine.Random;

public class LoopCtrl : MonoBehaviour
{
    public LoopListView2 mLoopListView;
    List<int[]> envCurData;

    public Button closeBtn;
    public Button saveBtn;
    public Button randomBtn;
    public Dropdown mDropdown;
    public InputField mInputField;
    public int reseCount;
    public int uSelect = 0;
    private List<int> testCaChe = new List<int>();
    public Dropdown mDropdownMode;

    List<int[]> saveCache = new List<int[]>();

    public void _Random()
    {
//        var list = RandomTime();

        envCurData = new List<int[]>();
        for (int i = 0; i < reseCount; i++)
        {
            List<int> ranges = new List<int>();
            ranges.AddRange(testCaChe);
            List<int> cacheList = new List<int>();
            for (int j = uSelect - 1; j >= 0; j--)
            {
                int index = Random.Range(0, ranges.Count);
                var value = ranges[index];
                ranges.RemoveAt(index);
//                ranges.Add();
                cacheList.Add(value);
            }

            cacheList.Sort();
            envCurData.Add(cacheList.ToArray());
        }
    }


    public List<DateTime> RandomTime()
    {
        int min_year = 1970;
        int max_year = 2021;

        int min_month = 1;
        int max_month = 13;

        int min_day = 1;
        int max_day = 28;
        int random_count = 0;

        int min_hour = 1;
        int max_hour = 25;

        int min_min = 0;
        int max_min = 61;

        int min_sec = 0;
        int max_sec = 61;
        List<DateTime> list = new List<DateTime>();
        while (list.Count != reseCount)
        {
            //年
            int year = Random.Range(min_year, max_year);
            //yue
            int month = Random.Range(min_month, max_month);
            //ri
            int day = Random.Range(min_day, max_day);

            int hour = Random.Range(min_hour, max_hour);

            int min = Random.Range(min_min, max_min);
            int sec = Random.Range(min_sec, max_sec);
            list.Add(new DateTime(year, month, day, hour, min, sec));
        }

        return list;
    }

    private bool init = false;

    public int BallLint = 33;

//    private 
    // Start is called before the first frame update
    void Start()
    {
        mDropdownMode.onValueChanged.AddListener((flag) =>
        {
            if (flag == 0)
            {
                BallLint = 33;
            }
            else
            {
                BallLint = 35;
            }
        });
        testCaChe.Clear();
        for (int i = 1; i <= 33; i++)
        {
            testCaChe.Add(i);
        }

        closeBtn.onClick.AddListener(() => { GameObject.Destroy(this.gameObject); });

        saveBtn.onClick.AddListener(() =>
        {
            if (mWriter != null)
            {
                mWriter.Dispose();
            }

            string fileName = "./temp_random.txt";
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            saveCache.AddRange(envCurData);

            mWriter = File.AppendText(fileName);
        });
        randomBtn.onClick.AddListener(() =>
        {
            int.TryParse(mInputField.text, out reseCount);
            uSelect = mDropdown.value + 1;

            _Random();
//            mLoopListView
            if (init == false)
            {
                mLoopListView.InitListView(reseCount, OnGetItemByIndex);
                init = true;
            }
            else
            {
                mLoopListView.SetListItemCount(envCurData.Count, false);
                mLoopListView.RefreshAllShownItem();
            }
        });
    }


    LoopListViewItem2 OnGetItemByIndex(LoopListView2 listView, int index)
    {
        if (index < 0 || index >= envCurData.Count)
        {
            return null;
        }

        LoopListViewItem2 item = listView.NewListViewItem("item");
        LoopProList itemScript = item.GetComponent<LoopProList>();
        itemScript.SetData(envCurData[index]);
        itemScript.OnInit();
        return item;
    }


    public List<int[]> saveData = new List<int[]>();
    public StreamWriter mWriter;

    // Update is called once per frame
    void Update()
    {
        if (saveCache.Count != 0)
        {
            for (int jIndex = 0; jIndex < 100; jIndex++)
            {
                var data = saveCache[0];
                saveCache.RemoveAt(0);
                string str = "";
                for (int i = 0; i < data.Length - 1; i++)
                {
                    str += $"{data[i]},";
                }

                str += $"{data[data.Length - 1]}";
                mWriter.WriteLine(str);

                if (saveCache.Count == 0)
                {
                    mWriter.Dispose();
                    mWriter = StreamWriter.Null;
                    return;
                }
            }
        }
    }
}